package com.gravata.enigma.adapter;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.gravata.enigma.R;
import com.gravata.enigma.model.Filtro;
import com.gravata.enigma.model.Regra;

public class RegrasAdapter extends BaseExpandableListAdapter {

	private Activity context;
	private List<Regra> regras;


	public class BouquetHolder{
	     public TextView text;
	}

	public RegrasAdapter(Activity context,List<Regra> regras) {
		this.context=context;
		this.regras=regras;
	}

	public void setData(List<Regra> regras) {
		this.regras = regras;
		notifyDataSetChanged();
	}

	@Override
	public Filtro getChild(int arg0, int arg1) {
		return regras.get(arg0).getFiltro(arg1);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return arg1;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean lastChild, View convertView,
			ViewGroup parent) {
			Filtro filtro=getChild(groupPosition, childPosition);
			BouquetHolder holder;
			LayoutInflater inflater = context.getLayoutInflater();
	        if (convertView == null) {

	            convertView = inflater.inflate(R.layout.row_item_filtro,null);
	            holder=new BouquetHolder();
	            holder.text=(TextView) convertView.findViewById(R.id.textView1);
	            convertView.setTag(holder);
	        }else{
	        	holder=(BouquetHolder) convertView.getTag();
	        }

	        holder.text.setText(filtro.getFiltro());

	        TextView ic=(TextView) convertView.findViewById(R.id.ic_negado);
	        if(filtro.isNegado())
	        	ic.setVisibility(View.VISIBLE);
	        else
	        	ic.setVisibility(View.INVISIBLE);


		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		try{
			return regras.get(groupPosition).getFiltros().size();
		}catch(Exception e){
			return 0;
		}
	}

	@Override
	public Regra getGroup(int groupPosition) {
		return regras.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return regras.size();
	}

	@Override
	public long getGroupId(int arg0) {
		return arg0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    	Regra regra = regras.get(groupPosition);

		BouquetHolder holder;
        if (convertView == null) {
        	LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.bouquet_section,null);

            holder=new BouquetHolder();
            holder.text=(TextView) convertView.findViewById(R.id.textView1);
            convertView.setTag(holder);
        }else{
        	holder=(BouquetHolder) convertView.getTag();
        }

        holder.text.setText(regra.getName());

       /* if(regra.isExclude()){
        	convertView.setBackgroundColor(convertView.getResources().getColor(R.color.exclude_color));
        }else if(regra.isFavorito()){
        	convertView.setBackgroundColor(convertView.getResources().getColor(R.color.favourites_color));
        }*/

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

}
